package com.example.auth.config.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.IOException;

public class SecurityContextDeserializer extends StdDeserializer<SecurityContext> {

    public SecurityContextDeserializer() {
        super(SecurityContext.class);
    }

    @Override
    public SecurityContext deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        SecurityContext context = SecurityContextHolder.createEmptyContext();
        
        if (node.has("authentication")) {
            Authentication authentication = p.getCodec().treeToValue(
                node.get("authentication"),
                Authentication.class
            );
            context.setAuthentication(authentication);
        }
        
        return context;
    }
} 